package com.tianqu.tms.www.member.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
/**
 * 会员账户信息修改结果页面
 * 
 * @author tianshifu
 *
 */
public class MemberUDAccResultController extends TmsController {
	
	private static final String ACCOUNTDATA = "AccountData";//会员信息
	
	private Logger logger = Logger.getLogger(MemberUDAccResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		//添加事务提交异常捕获
		try {
			controller.begin();
			TmsUserProfile profile = uc.getUserProfile();

			//从uc中获取修改后的账户信息，在member表中进行更新
			MemberInputViewHelper view = (MemberInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);	
			Member member = Member.getInstance();
			member = member.findMemberInfoByMemId(view.getMemberId());
			member.setAucMargin(member.getAucMargin());
			member.setMargin(StringUtil.removeDH(view.getMargin()));
			member.setVirtualMargin(StringUtil.removeDH(view.getVirtualMargin()));
			member.setMemId(Integer.parseInt(view.getMemberId()));
			member.setUpdateUserId(profile.getUniqueId());
			//执行更新
			member.updateAccountInfoByMemId(member);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR,e1);
			}
			//系统异常
			if(e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR,e);
		}

	}

}
